import { Button, Flex, Input } from "antd";
import React, { useState } from "react"
import { runProg } from "../conn/utils";
import conn from '../conn/conn';

function parseHexStr(text: string) {
	const as = text.split(' ');
	return as.map((item) => { return parseInt(item.trim(), 16) & 0xFF });
}

const WriteTest: React.FC = () => {
	const [text, setText] = useState('');

	const onSend = async () => {
		await runProg('发送数据', async () => {
			if (text.length == 0) {
				throw '请输入数据';
			}
			const buf = parseHexStr(text);
		//	console.log(buf);
			await conn.write( buf );
		})

	}

	return (
		<Flex vertical={true} gap={10} align={"center"}>
			<p>发送测试</p>
			<Flex vertical={false} gap={10}>
				<Input allowClear={true} style={{width: "250px"}} value={text} onChange={(e) => setText(e.target.value)}></Input>
				<Button onClick={onSend} type="primary">发送</Button>
			</Flex>
		</Flex>

	)
}

export default WriteTest;
